package com.mzy.linear_struct.subject;

import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author: mizy3
 * @Date: 2024/07/26/上午10:36
 * @Description:
 */
public class 全排列2 {

    public List<List<Integer>> permuteUnique(int[] nums) {

        List<List<Integer>> res = new ArrayList<>();
        dfs(nums, new boolean[nums.length], new LinkedList<Integer>(), 0, res, nums.length);
        return res;
    }


    public void dfs(int[] nums, boolean[] used, Deque<Integer> path, int depth,
                    List<List<Integer>> res, int len) {
        if (len == depth) {
            res.add(new ArrayList<>(path));
            return;
        }

        for (int i = 0; i < len; i++) {
            if(!used[i]){
                if(i > 0 && nums[i] == nums[i - 1]&& used[i - 1] == false){
                    continue;
                }

                path.addLast(nums[i]);
                used[i] = true;
                dfs(nums, used, path, depth + 1, res, len);
                used[i] = false;
                path.removeLast();
            }
        }
    }

    public static void main(String[] args) {
        全排列2 全排列2 = new 全排列2();
        全排列2.permuteUnique(new int[]{1,1,2});
    }
}
